/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/12
 * Time: 15:43
 */
$(function(){

    $('.apart-id').select2();

    sendAjax('/v1/manager/user/contract/list.json', 'get', {}, function(json){
        var contracts = json.body.rows;
        $.each(contracts, function(n, v){
            var option = '<option value="'+ v.apart_id+'" data-user-id="'+v.user_id+'" data-user-name="'+v.realname+'">'+ v.apart_name+'</option>';
            $('.apart-id').append(option);
        });
        if (contracts.length > 0) {
            $('.select2-selection__rendered').attr('title', contracts[0].apart_name).text(contracts[0].apart_name);
            $('.rent-user').text(contracts[0].realname);
            $('.user-id').val(contracts[0].user_id);
            sendAjax('/v1/manager/fee/last.json', 'get', {type:1, apart_id:contracts[0].apart_id, user_id:contracts[0].user_id}, function(json){
                var fee = json.body.data;
                if (fee) {
                    $('.start-end-time').val(fee.end_at+' ~');
                    $('.input-last-process').val(fee.process);
                }
            });
        }
    });

    $('.apart-id').on('change', function(){
        var realname = $(this).find('option:selected').attr('data-user-name');
        var user_id = $(this).find('option:selected').attr('data-user-id');
        $('.rent-user').text(realname);
        $('.user-id').val(user_id);
        $('.start-end-time').val('');
        $('.input-last-process').val('');
        $('.process').val('');
        $('.num').text('');
        $('.price').val('');
        $('.fee').text('');
        sendAjax('/v1/manager/fee/last.json', 'get', {type:2, apart_id:$(this).val(), user_id:user_id}, function(json){
            var fee = json.body.data;
            if (fee) {
                $('.start-end-time').val(fee.end_at+' ~');
                $('.input-last-process').val(fee.process);
                $('.price').val(fee.price);
            }
        });
    });

    $('.process').on('change', function(){
        var num = $('.process').val() - $('.input-last-process').val();
        $('.num').text(num.toFixed(2));
        if ($('.price').val() != '') {
            var fee = $('.price').val() * num;
            $('.fee').text(fee.toFixed(2));
        }
    });

    $('.price').on('change', function(){
        var fee = $(this).val();
        if ($('.num').text() != '') {
            fee *= $('.num').text();
            $('.fee').text(fee.toFixed(2));
        }
    });

    $('.btn-save').on('click', function(){
        var data = {
            type: $('.fee-type').val(),
            apart_id: $('.apart-id').val(),
            user_id: $('.user-id').val(),
            last_process: $('.input-last-process').val(),
            process: $('.process').val(),
            price: $('.price').val(),
            start_end_time: $('.start-end-time').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/save/fee', 'post', {data:data}, function(json){
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.href = '/manager/fee/water.html';
    });
});

function checkForm(data) {
    if (!data.type) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.apart_id) {
        $('#action-tip .modal-body .tip-content').text('请选择房源');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.user_id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.last_process) {
        $('#action-tip .modal-body .tip-content').text('请填写上次抄表数');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.process) {
        $('#action-tip .modal-body .tip-content').text('请填写本次抄表数');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.price) {
        $('#action-tip .modal-body .tip-content').text('请填写价格');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.start_end_time) {
        $('#action-tip .modal-body .tip-content').text('请填写起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (data.start_end_time.split(' ~ ').length < 2) {
        $('#action-tip .modal-body .tip-content').text('请选择起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (new Date(data.start_end_time.split(' ~ ')[1]) < new Date(data.start_end_time.split(' ~ ')[0])) {
        $('#action-tip .modal-body .tip-content').text('开始时间不能大于结束时间');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}